/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.struts.actions;

import com.oea.hibernate.entities.Branch_Subject;
import com.oea.hibernate.entities.Subject;
import com.oea.struts.forms.AddSubjectForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author unnati
 */
public class AddSubjectAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private  String  path;

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        AddSubjectForm asf = (AddSubjectForm) form;

        Subject s = new Subject();
        s.setName(asf.getName());
        s.setAddedby(asf.getAddedby());
        s.setLabsperweek(asf.getLabsperweek());
        s.setLecturesperweek(asf.getLecturesperweek());

        s = (Subject) s.add();

        Subject sub = (Subject) s.GetByName();
        Long subid = sub.getId();

        Branch_Subject bs = new Branch_Subject();
        bs.setBid(asf.getBid());
        System.out.println("grs"+asf.getBid());
        bs.setSemester(asf.getSemester());
        bs.setSubid(subid);
       
        bs = (Branch_Subject) bs.add();

        if (bs != null) {
            
        
            if (s != null) {
                List<Subject> li_allsubjects = (List<Subject>) sub.getAll();
                request.getServletContext().setAttribute("li_allsubjects", li_allsubjects);
                request.getServletContext().setAttribute("page_li_allsubjects", li_allsubjects);
                for (Subject subject : li_allsubjects) {
                    subject.setAdmin();
                }

                double pagelink = Math.ceil((float) li_allsubjects.size() / 10);
                System.out.println("pagination" + pagelink);
                request.getSession().setAttribute("pagelink", pagelink);
                path = "view_subject";
            }
        }
       
        return mapping.findForward(path);
    }
}
